use std::cell::RefCell;
use std::rc::{Rc, Weak};

#[derive(Debug)]
struct Node {
    value: i32,
    parent: RefCell<Weak<Node>>,
    child: RefCell<Vec<Rc<Node>>>
}

fn main() {
    let leaf = Rc::new(Node{
        value: 3,
        parent: RefCell::new(Weak::new()),
        child: RefCell::new(vec![]),
    });
    println!("1 leaf string = {}, 1 leaf weak = {}", Rc::strong_count(&leaf), Rc::weak_count(&leaf));

    let branch = Rc::new(Node{
        value: 5,
        parent: RefCell::new(Weak::new()),
        child: RefCell::new(vec![Rc::clone(&leaf)]),
    });
    println!("1 branch string = {}, 1 branch weak = {}", Rc::strong_count(&branch), Rc::weak_count(&branch));

    *leaf.parent.borrow_mut() = Rc::downgrade(&branch);
    println!("2 branch string = {}, 2 branch weak = {}", Rc::strong_count(&branch), Rc::weak_count(&branch));
    println!("2 leaf string = {}, 2 leaf weak = {}", Rc::strong_count(&leaf), Rc::weak_count(&leaf));
    println!("Hello, world!");
}
